﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.BLL;
using InstrumentCheck.Module;
using InstrumentCheck.ILocalService;
using System.Workflow;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;

namespace InstrumentCheck.WebAppUI
{
    public partial class ShowRejectRouting : BasePage
    { 
        protected void Page_Load(object sender, EventArgs e)
        {
            string formno = Request["NO"].ToString();
            string strComment = Request["comment"].ToString();
            
            int intFormNO = int.Parse(formno);
            txtRemark.Text = strComment;
           
            //根据FormNO到table中捞出现在已经签到哪关了，然后show出来
            List<FormRouting> list = Form_bll.GetRejectDistinctRouting(intFormNO);
       
            FormRouting routing = new FormRouting();
            routing.FlowNO = 0;
            routing.FlowName = "申請者";
            User Receiver = new User();
            Receiver.EmpChineseName = "申請者";
            routing.Receiver = Receiver;
            list.Insert(0, routing);
            //list.Add(routing);           

            gvRoutingHistory.DataSource = list;
            gvRoutingHistory.DataBind();
        }
               
        protected void gvRoutingHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                FormRouting routing = e.Row.DataItem as FormRouting;
                e.Row.Cells[2].Text = routing.Receiver.EmpChineseName;

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E0E0E0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                e.Row.Attributes["style"] = "Cursor:hand";
                e.Row.Attributes.Add("onclick", "Reject('" + PublicFunction.Decode(e.Row.Cells[0].Text) + "','" + txtRejectToFlow.ClientID + "')");
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            FormRouting routing = Session["Routing"] as FormRouting;
            ApplyForm form = Session["Form"] as ApplyForm;
            User approver = Session["UserInfo"] as User;
            routing.Approver = approver;
            routing.ApproverDate = DateTime.Now;
            routing.ApproveResult = ApproveResult.Disagree;
            routing.ApproveStatus = ApproveStatus.Approved;
            routing.Remark = txtRemark.Text.Trim();
            routing.FormNO = form.FormNO;

            try
            {
                Form_bll.Reject(form, routing, int.Parse(txtRejectToFlow.Text.Trim()), this.PageURL(), approver.Email);              
            }
            catch(Exception ex)
            {
                this.AlertMessage("簽核過程中發生錯誤！錯誤信息：" + ex.Message);
                return;
            }
            this.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "closeWindow()", true);
        }
    }
}
